<!-- 博客 RSS 和 微语 RSS 混合展示 -->
<div id="result-loading" style="display: grid; place-items: center;"><i class="fa fa-spinner fa-pulse fa-3x fa-fw">
    <svg xmlns="http://www.w3.org/2000/svg" width="5em" height="5em" viewBox="0 0 256 256"><path fill="currentColor" d="M128 24a104 104 0 1 0 104 104A104.11 104.11 0 0 0 128 24m39.11 25.19C170.24 83.71 155 99.44 135 113.61c-2.25-24.48-8.44-49.8-38.37-67.82a87.89 87.89 0 0 1 70.5 3.4ZM40.18 133.54c28.34-20 49.57-14.68 71.87-4.39c-20.05 14.19-38.86 32.21-39.53 67.11a87.92 87.92 0 0 1-32.34-62.72m136.5 67.73c-31.45-14.55-37.47-35.58-39.71-60c12.72 5.86 26.31 10.75 41.3 10.75c11.33 0 23.46-2.8 36.63-10.08a88.2 88.2 0 0 1-38.22 59.33"/></svg>
</i>
</div>
<span>来自 <a href="/posts/">文章</a> 和 <a href="/whispers/">微语</a> RSS</span>
<div style="float:right;">
    <a id="whispers-posts">最新动态</a>:
    <a id="only-posts">文章</a> /
    <a id="only-whispers">微语</a>
</div>
<div id="result"></div>
<script type="text/javascript" src="/js/time-fmt.min.js"></script>
<script type="text/javascript" src="https://cdn.ftls.xyz/static/whispers/assets/js/view-image.min.js"></script>
<script>
    let items1 = null;
    let items2 = null;
    async function fetchBlogAndWhispersItems() {
        // const url1 = 'https://www.ftls.xyz/index.xml'; // 第一个XML数据URL  
        const url1 = '/index.xml'; // 第一个XML数据URL  
        const url2 = 'https://gotosocial-kkbt-tools-xkzlczgovq.cn-hangzhou.fcapp.run/@kkbt/feed.rss'; // 第二个XML数据URL  

        const response1 = await fetch(url1);
        const data1 = await response1.text();
        const channel1 = (new DOMParser()).parseFromString(data1, 'text/xml').querySelector('channel');

        const response2 = await fetch(url2);
        const data2 = await response2.text();
        const channel2 = (new DOMParser()).parseFromString(data2, 'text/xml').querySelector('channel');

        if (channel1 && channel2) {
            items1 = [...channel1.querySelectorAll('item')];
            items2 = [...channel2.querySelectorAll('item')];
        } else {
            console.log('No channel found!');
            document.getElementById("result-loading").innerHTML = "请求错误!"
        }
    }

    function showItems(items) {
        const result = document.getElementById('result');
        items.slice(0, 5).forEach((item) => {
            let guid = item.querySelector('guid').textContent;
            const pubDate = item.querySelector('pubDate').textContent;
            if (guid.startsWith('https://fmb.ftls.xyz')) {
                guid = '/whispers/?id=' + guid.slice('https://fmb.ftls.xyz/@kkbt/statuses/'.length);
                const content = item.getElementsByTagName('content:encoded')[0].textContent;
                result.innerHTML += `<small><strong>#微语</strong> ${formatTime(pubDate)}</small><br>${content.replace(/<img/g, '<img loading="lazy" ')}<a href='${guid}'>查看微语</a><hr style="margin:.5rem 0">`
            } else {
                const title = item.querySelector('title').textContent;
                const description = item.querySelector('description').textContent;
                result.innerHTML += `<small><strong>#文章</strong> ${formatTime(pubDate)}</small><br><strong><a href="${guid}" style="color: inherit;">${title}</a></strong>${description} <a href='${guid}'>阅读文章</a><hr style="margin:.5rem 0">`
            }
        });
    }

    function defaultShow() {
        // 合并两个数组，根据 pubDate排序  
        const sortedItems = [...items1, ...items2].sort((a, b) => new Date(b.querySelector('pubDate').textContent) - new Date(a.querySelector('pubDate').textContent));
        showItems(sortedItems);
    }

    fetchBlogAndWhispersItems().then(() => {
        document.getElementById("result-loading").style.display = "none";
        defaultShow()
    });
    // 画廊
    window.ViewImage && ViewImage.init('p img');

    // 监听点击事件
    document.getElementById("whispers-posts").addEventListener("click", function (event) {
        document.getElementById('result').innerHTML = "";
        defaultShow();
    })
    document.getElementById("only-posts").addEventListener("click", function (event) {
        document.getElementById('result').innerHTML = "";
        showItems(items1);
    })
    document.getElementById("only-whispers").addEventListener("click", function (event) {
        document.getElementById('result').innerHTML = "";
        showItems(items2);
    })
</script>